package edu.whu.xterm.activity;

import edu.whu.xterm.R;
import greendroid.widget.GActionBar;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class BoardsActivity extends ExpandableListActivity {

	static final String[][] BOARDS = {
			{ "Astrology", "Automobile", "dancing", "Debate", "Family", "Fashion", "Feeling",
					"food", "FreeTalk", "Game", "Humor", "KeepFit", "KingKiller", "Love", "Movie",
					"Mud", "Picture", "PieFriends", "PopMusic", "Riddle", "Rock", "single",
					"Travel", "TV", "Youth" },
			{ "PostGraduate", "Teachers", "WHDXL", "WHUCelebration", "WHUCentury", "WHUConnection",
					"WHUExpress", "WHUResource" },
			{ "Chorus", "Classics", "Comic", "Emprise", "English", "French", "MyStage", "Novels",
					"Photography", "Poetry", "Reader", "S.F.", "SanGuo", "StoneStory", "Story" },
			{ "Anhui", "BaShu", "Beijing", "Fujian", "Gansu", "Guangdong", "Guangxi", "Guizhou",
					"Hebei", "Henan", "Hubei", "Hunan", "Jiangsu", "Jiangxi", "NorthEast",
					"Shaanxi", "Shandong", "Shanghai", "Wuhan", "Zhejiang" },
			{ "Abroad", "ADagent_TG", "EnglishTest", "Graduate", "House", "Job", "JobInfo",
					"kaoyan", "Medicine", "Military", "MyWallet", "News", "PartTimeJob", "Search",
					"secondhand", "TrafficInfo", "WHU", "Wish" },
			{ "Badminton", "baseball", "Basketball", "Bicycle", "Bridge", "Chess", "Football",
					"GO", "Olympic2008", "Sports", "Tabletennis", "Tennis", "Volleyball", "Yoga" },
			{ "ACM_ICPC", "BBSDev", "CPlusPlus", "Database", "Digital", "Hardware", "IBM",
					"Internet", "Java", "Linux_Unix", "Microsoft", "OS", "Program", "Security",
					"Software", "TeX", "Theory" },
			{ "Biology", "Chemistry", "Economics", "Electronics", "Environment", "Geography",
					"History", "Law", "Math", "NumComp", "Physics", "Science" },
			{ "Advice", "Announce", "BBSData", "notepad", "BBSHelp", "Cherub", "Recommend", "Test" } };

	ExpandableListAdapter mAdapter;

	private GActionBar actionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		actionBar = ((MainTabActivity) getParent()).getGActionBar();
		mAdapter = new BoardsListAdapter();
		setListAdapter(mAdapter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		actionBar.setTitle(getString(R.string.tab_board_title));
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
			int childPosition, long id) {
		String desc = (String) mAdapter.getChild(groupPosition, childPosition);
		String name = BOARDS[groupPosition][childPosition];
		Intent intent = new Intent(getApplicationContext(), BoardArticleListActivity.class);
		intent.putExtra(BoardArticleListActivity.KEY_BOARD_DESC, desc);
		intent.putExtra(BoardArticleListActivity.KEY_BOARD_NAME, name);
		startActivity(intent);
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
		int type = ExpandableListView.getPackedPositionType(info.packedPosition);
		if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
			int group = ExpandableListView.getPackedPositionGroup(info.packedPosition);
			int child = ExpandableListView.getPackedPositionChild(info.packedPosition);
		}
	}

	public class BoardsListAdapter extends BaseExpandableListAdapter {

		private String[] groups;
		private String[][] children;

		public BoardsListAdapter() {
			groups = getResources().getStringArray(R.array.farther_boards);
			children = new String[groups.length][];
			children[0] = getResources().getStringArray(R.array.child_boards_leisure);
			children[1] = getResources().getStringArray(R.array.child_boards_whu);
			children[2] = getResources().getStringArray(R.array.child_boards_literature);
			children[3] = getResources().getStringArray(R.array.child_boards_fellow);
			children[4] = getResources().getStringArray(R.array.child_boards_socieity);
			children[5] = getResources().getStringArray(R.array.child_boards_sports);
			children[6] = getResources().getStringArray(R.array.child_boards_it);
			children[7] = getResources().getStringArray(R.array.child_boards_science);
			children[8] = getResources().getStringArray(R.array.child_boards_system);
		}

		@Override
		public int getGroupCount() {
			return groups.length;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return children[groupPosition].length;
		}

		@Override
		public String getGroup(int groupPosition) {
			return groups[groupPosition];
		}

		@Override
		public String getChild(int groupPosition, int childPosition) {
			return children[groupPosition][childPosition];
		}

		@Override
		public long getGroupId(int groupPosition) {
			return 0;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return 0;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
				ViewGroup parent) {
			TextView textView;
			if (convertView == null) {
				textView = getGenericView();
			} else {
				textView = (TextView) convertView;
			}
			textView.setText(getGroup(groupPosition));

			return textView;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
				View convertView, ViewGroup parent) {
			TextView textView;
			if (convertView == null) {
				textView = getGenericView();
			} else {
				textView = (TextView) convertView;
			}
			textView.setText(getChild(groupPosition, childPosition).toString());
			return textView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		public TextView getGenericView() {
			AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT, 64);
			TextView textView = new TextView(getApplicationContext());
			textView.setLayoutParams(lp);
			textView.setGravity(Gravity.CENTER_VERTICAL);
			textView.setPadding(55, 0, 0, 0);
			return textView;
		}

	}

}
